package com.loveprogrammer.handler.support;

import com.alibaba.fastjson2.JSON;
import com.loveprogrammer.command.anotation.TagListener;
import com.loveprogrammer.command.anotation.TopicListener;
import com.loveprogrammer.command.client.ClientLoginTag;
import com.loveprogrammer.command.client.ClientMenuTag;
import com.loveprogrammer.command.client.ClientTopic;
import com.loveprogrammer.command.server.ServerHeroTag;
import com.loveprogrammer.command.server.ServerLoginOrRegisterTag;
import com.loveprogrammer.command.server.ServerTopic;
import com.loveprogrammer.console.ConsolePrint;
import com.loveprogrammer.constants.CommonValue;
import com.loveprogrammer.dto.base.StringRequest;
import com.loveprogrammer.dto.job.HeroCreate;
import com.loveprogrammer.dto.job.JobChoose;
import com.loveprogrammer.dto.login.UserLogin;
import com.loveprogrammer.dto.login.UserRegister;
import com.loveprogrammer.handler.BaseHandler;
import com.loveprogrammer.pojo.StringMessage;
import com.loveprogrammer.utils.ChannelUtils;
import com.loveprogrammer.utils.ScannerInput;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Scanner;

/**
 * @ClassName MenuHandler
 * @Description TODO
 * @Author admin
 * @Date 2024/2/18 17:37
 * @Version 1.0
 */
@TopicListener(topic = ClientTopic.TOPIC_LOGIN)
public class LoginHandler extends BaseHandler {

    public static final Logger log = LoggerFactory.getLogger(LoginHandler.class);

    @TagListener(tag = ClientLoginTag.TAG_LOGIN_REGISTER, messageClass = StringRequest.class)
    public void LoginMenu(ChannelHandlerContext ctx, StringRequest msg) {
        // 展示首页数据
        ConsolePrint.publishMessage("【1.登录已有账户】  【2.注册新账户】  【3.退出】");
        ConsolePrint.publishMessage("请选择：");

        int choose = ScannerInput.inputInt(1, 3, 3);

        if(choose != 3) {
        switch (choose) {
            case 1:
                ConsolePrint.publishMessage("请输入账户名：");
                String name = ScannerInput.inputString();
                ConsolePrint.publishMessage("请输入密码：");
                String password = ScannerInput.inputString();
                UserLogin userLogin = new UserLogin(ClientTopic.TOPIC_LOGIN, ClientLoginTag.TAG_LOGIN_CHOOSE_HERO
                        ,ClientTopic.TOPIC_LOGIN,ClientLoginTag.TAG_LOGIN_REGISTER);
                userLogin.setUserName(name);
                userLogin.setPassword(password);
                StringMessage message = new StringMessage();
                message.setTopicId(ServerTopic.TOPIC_LOGIN);
                message.setTagId(ServerLoginOrRegisterTag.TAG_LOGIN_LOGIN);
                message.setStatusCode(CommonValue.MSG_STATUS_CODE_SUCCESS);
                message.setBody(JSON.toJSONString(userLogin));
                ChannelUtils.pushToServer(ctx.channel(), message);
                return;
            case 2:
                // 注册
                ConsolePrint.publishMessage("请输入注册的用户名");
                String registerName = ScannerInput.inputString();
                UserRegister register = new UserRegister(ClientTopic.TOPIC_LOGIN, ClientLoginTag.TAG_LOGIN_CHOOSE_HERO
                        ,ClientTopic.TOPIC_LOGIN,ClientLoginTag.TAG_LOGIN_REGISTER);
                register.setName(registerName);
                StringMessage data = StringMessage.create(ServerTopic.TOPIC_LOGIN, ServerLoginOrRegisterTag.TAG_LOGIN_REGISTER);
                data.setStatusCode(CommonValue.MSG_STATUS_CODE_SUCCESS);
                data.setBody(JSON.toJSONString(register));
                ChannelUtils.pushToServer(ctx.channel(), data);
                return;
            default:
                ConsolePrint.publishMessage("暂未开放，敬请期待", 1);
                return;
        }}
        System.exit(0);
    }


    /***
     * 选择职业
     * @param ctx
     * @param data
     */
    @TagListener(tag = ClientLoginTag.TAG_LOGIN_CHOOSE_HERO, messageClass = JobChoose.class)
    public void chooseHero(ChannelHandlerContext ctx, JobChoose data) {
        //
        ConsolePrint.publishMessage("请选择要创建的职业");
        List<JobChoose.JobItem> jobList = data.getJobList();
        for (int i = 0; i < jobList.size(); i++) {
            ConsolePrint.publishMessage((i+1) + ":" + jobList.get(i).toString(), 1);
        }
        Scanner input = new Scanner(System.in);
        int choose = input.nextInt();
        while(choose < 0 || choose > jobList.size()){
            ConsolePrint.publishMessage("请重新选择", 1);
            choose = input.nextInt();
        }
        JobChoose.JobItem jobItem = jobList.get(choose - 1);
        ConsolePrint.publishMessage("选择的职业为" + jobItem.getName());
        ConsolePrint.publishMessage("请输入角色的名称：");
        String heroName = ScannerInput.inputString();

        // 创建角色
        StringMessage message = StringMessage.create(ServerTopic.TOPIC_HERO, ServerHeroTag.TAG_HERO_CREATE);
        HeroCreate create = new HeroCreate(ClientTopic.TOPIC_MENU, ClientMenuTag.TAG_MENU_JOB_CHOOSE, ClientTopic.TOPIC_LOGIN, ClientLoginTag.TAG_LOGIN_CHOOSE_HERO);
        create.setId(jobItem.getId());
        create.setHeroName(heroName);
        message.setStatusCode(CommonValue.MSG_STATUS_CODE_SUCCESS);
        message.setBody(JSON.toJSONString(create));
        ChannelUtils.pushToServer(ctx.channel(), message);
    }
}
